<?php
//=======================================
//###################################
// Kayako Web Solutions
//
// Source Copyright 2001-2004 Kayako Web Solutions
// Unauthorized reproduction is not allowed
// License Number: $%LICENSE%$
// $Author: vshoor $ ($Date: 2005/06/21 23:53:39 $)
// $RCSfile: plexum.login.php,v $ : $Revision: 1.2 $ 
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
//###################################
//=======================================

if (!defined("INSWIFT")) {
	trigger_error("Unable to process $PHP_SELF", E_USER_ERROR);
}

/**
* Initialization function. You can connect to your database etc over here.
*/
function loginShareInit()
{
	global $loginshare;

	$loginshare->moduleloaded = true;
}

/**
* Authorize a user based on email and password
*/
function loginShareAuthorize($username, $password)
{
	global $dbCore, $_SWIFT, $loginshare, $settings;

	$_loginshare = $settings->getSection("loginshare");
	
	$plxdbName = explode("_", $_loginshare["plxdbname"]);
	$plxVersion = $plxdbName['0'];
	$plxEncode = $plxdbName['1'];
	$plxDBName = str_replace(array("$plxdbName[0]_","$plxdbName[1]_"), "", $_loginshare["plxdbname"]);
	$dbLoginShare = new dbCore($_loginshare["plxhostname"], $_loginshare["plxdbuser"], $_loginshare["plxdbpass"], $plxDBName, "mysql", false);

	$_user = array();
	if ($plxVersion == 'x4' || $plxVersion == 'p3') {
		$_user = $dbLoginShare->queryFetch("SELECT *, m_password AS m_passwd FROM `members` WHERE `m_id` = '". $dbCore->escape($username) ."';");
	} else {
	    $_user = $dbLoginShare->queryFetch("SELECT *, DECODE(m_password,'$plxEncode') AS m_passwd FROM `members` WHERE `m_id` = '". $dbCore->escape($username) ."';");
	}
	
	if (empty($_user["m_id"]))
	{
		return false;
	}
	
  $_user['m_fname'] = stripslashes($_user['m_fname']);
  $_user['m_lname'] = stripslashes($_user['m_lname']);

	$userpassword = md5($password);

	$regpassword = $_user["m_passwd"];

	if ($_user["m_passwd"] == $userpassword && !empty($_user["m_passwd"]))
	{
		$userid = getLoginShareUser(LOGINAPI_PLEXUM, $_user["m_id"]);
		if (!$userid)
		{
			// Not registered, Register him
			$userid = insertUser(true, $_user["m_email"], $regpassword, $_SWIFT["tgroup"]["regusergroupid"], LOGINAPI_PLEXUM, $_user["m_id"], $_user["m_fname"]." ".$_user["m_lname"], $_SWIFT["tgroup"]["languageid"], 0, false, 1, true, true);
		}

		if (!$userid)
		{
			return false;
		}

		$_swiftuser = $loginshare->loadSWIFTUser($userid);
		if (!$_swiftuser)
		{
			return false;
		}

		$_SWIFT["user"] = $_swiftuser;

		return $_swiftuser["userid"];
	}

	return false;
}

/**
* Return the Unique User ID of the current user
*/
function loginShareUserID()
{
	global $_SWIFT;

	if (empty($_SWIFT["user"]["userid"]))
	{
		return false;
	} else {
		return $_SWIFT["user"]["userid"];
	}
}

/**
* Logout the current user
*/
function loginShareLogout()
{
	global $session, $_SWIFT;

	$session->updateSession($_SWIFT["session"]["sessionid"], 0);

	return true;
}

/**
* Load the user credentials into current workspace. The following variables should be declared for proper working:
* userid - User id that is set in the "users" table
* fullname
* email - Array
* password (MD5 Hashed)
* usergroupid - If this is not set, then it will use the default registered user group for this template group
*/
function loginShareLoadUser()
{
	global $dbCore, $_SWIFT, $loginshare;

	if (empty($_SWIFT["session"]["typeid"]))
	{
		$_SWIFT["user"]["loggedin"] = false;
		return false;
	}

	$_user = $loginshare->loadSWIFTUser($_SWIFT["session"]["typeid"]);
	if (!$_user)
	{
		$_SWIFT["user"]["loggedin"] = false;

		return false;
	}

	$_SWIFT["user"] = $_user;

	return true;
}

/**
* Renders the Login Share Form
*/
function renderLoginShareForm()
{
	global $_SWIFT;

	$forms = array();

	$forms[0]["title"] = $_SWIFT["language"]["hostname"];
	$forms[0]["name"] = "plxhostname";
	$forms[1]["title"] = $_SWIFT["language"]["dbname"];
	$forms[1]["name"] = "plxdbname";
	$forms[2]["title"] = $_SWIFT["language"]["dbuser"];
	$forms[2]["name"] = "plxdbuser";
	$forms[3]["title"] = $_SWIFT["language"]["dbpass"];
	$forms[3]["name"] = "plxdbpass";

	return $forms;

}
?>